package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.dto.type.TypeQueryResponse;
import online.longzipeng.cinemasys.entity.Type;
import online.longzipeng.cinemasys.service.TypeService;
import online.longzipeng.cinemasys.vo.type.TypeQueryVO;
import online.longzipeng.cinemasys.vo.type.TypeSaveVO;
import online.longzipeng.cinemasys.vo.type.TypeUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  分类控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/type")
@Api(value = "分类相关接口",description = "分类相关接口",tags = "分类相关接口")
public class TypeController {

    @Autowired
    TypeService typeService;

    @RequestMapping(value = "/get-list-page",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation("查询所有分类带分页")
    public Result getListPage(@RequestBody TypeQueryVO queryVO){
        IPage<TypeQueryResponse> typeIPage = typeService.getListPage(queryVO);
        return Result.success(typeIPage);
    }

    @GetMapping(value = "/get-All-level-one-Json")
    @ApiOperation("查询所有一级分类")
    public Result getAllLevelOneJson(){
        List<Type> types = typeService.getAllLevelOneJson();
        return Result.success(types);
    }

    @GetMapping(value = "/get-All-level-two-Json")
    @ApiOperation("查询所有二级分类")
    public Result getAllLevelTwoJson(){
        List<Type> types = typeService.getAllLevelTwoJson();
        return Result.success(types);
    }

    @GetMapping(value = "/get-All-level-two-Json-by-level-one-id")
    @ApiOperation("通过一级分类id查询二级分类")
    public Result getAllLevelTwoJsonByLevelOneId(@RequestParam(value = "id",required = true) Long id){
        List<Type> types = typeService.getAllLevelTwoJsonByLevelOneId(id);
        return Result.success(types);
    }

    @PostMapping("/save")
    @NeedPermissions("type:save")
    @ApiOperation("新增分类")
    public Result save(@Valid @RequestBody TypeSaveVO typeSaveVO){
        Type type = new Type();
        BeanUtils.copyProperties(typeSaveVO,type);
        typeService.add(type);
        return Result.success(null,"分类添加成功！！");
    }

    @GetMapping("/get-by-id")
    @NeedPermissions("type:select")
    @ApiOperation("通过id获取分类")
    public Result getById(@ApiParam(value = "分类Id",required = true)Long id){
        return Result.success(typeService.getById(id));
    }

    @GetMapping("/delete-one")
    @NeedPermissions("type:delete")
    @ApiOperation("删除分类")
    public Result deleteOne(@ApiParam(value = "分类id",required = true)Long id){
        try {
            typeService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"分类删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("type:delete")
    @ApiOperation("删除多个分类")
    public Result deleteOne(@ApiParam(value = "分类id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            typeService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"分类删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("type:update")
    @ApiOperation("修改分类")
    public Result update(@Valid @RequestBody TypeUpdateVO typeUpdateVO) {
        Type type = new Type();
        BeanUtils.copyProperties(typeUpdateVO,type);
        typeService.updateType(type);
        return Result.success(null,"分类修改成功！！");
    }
}
